<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 订单搜索和添加区域 -->
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getOrderList">
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单数据table区域 -->
      <el-table :data="orderList" stripe border style="width: 100%">
        <el-table-column prop="#" type="index"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格(元)" width="100"></el-table-column>
        <el-table-column prop="goods_weight" label="是否付款" width="80">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.order_pay === 1">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="80"></el-table-column>
        <el-table-column label="下单时间" width="160">
          <template slot-scope="scope">{{scope.row.create_time|dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="修改订单地址" placement="bottom-start">
              <el-button size="mini" type="primary" icon="el-icon-edit" @click="showAddrDialog"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="物流信息" placement="bottom-start">
              <el-button
                size="mini"
                type="success"
                icon="el-icon-location"
                @click="showProgressDialog(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 订单数据翻页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 修改收获地址对话框 -->
    <el-dialog title="修改地址" :visible.sync="addrDialogVisible" width="50%" @close="addrDialogClosed">
      <!-- 修改地址表单 -->
      <el-form :model="addrForm" :rules="addrFormRules" ref="addrFormRef" label-width="100px">
        <el-form-item label="省市区/县" prop="addr1">
          <el-cascader
            :options="cityData"
            :props="cascaderProps"
            v-model="addrForm.addr1"
            @change="handleChange"
            :clearable="true"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="addr2">
          <el-input v-model="addrForm.addr2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="addrDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addrDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 显示物流进度对话框 -->
    <el-dialog title="查看物流进度" :visible.sync="progressDialogVisible" width="50%">
      <el-steps direction="vertical" :active="0" :space="30">
        <el-step v-for="item in progressinfo" :key="item.time" :title="item.time" :description="item.context"></el-step>
      </el-steps>
    </el-dialog>
  </div>
</template>
<script>
import cityData from './citydata.js'
export default {
  data() {
    return {
      // 获取订单列表参数对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 订单列表数据数组
      orderList: [],
      // 订单列表总的条目数
      total: 0,
      // 控制修改地址对话框的显示与隐藏
      addrDialogVisible: false,
      // 修改地址表单数据对象
      addrForm: {
        addr1: [],
        addr2: ''
      },
      // 修改地址表单数据验证规则对象
      addrFormRules: {
        addr1: [{ required: true, message: '请选择省市区县', trigger: 'blur' }],
        addr2: [
          { required: true, message: '请填写详细的物流地址', trigger: 'blur' }
        ]
      },
      cityData: cityData,
      // 指定级联选择器的配置对象
      cascaderProps: {
        expandTrigger: 'hover',
        value: 'value',
        label: 'label',
        children: 'children'
      },
      // 控制物流对话框的显示与隐藏
      progressDialogVisible: false,
      // 物流信息数据数组
      progressinfo: []
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    // 获取订单列表数据
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表数据失败！')
      }
      // 为订单列表数据赋值
      this.orderList = res.data.goods
      // 为订单总的条目数赋值
      this.total = res.data.total
      this.$message.success('获取订单列表数据成功！')
      console.log(this.orderList)
    },
    handleSizeChange(newsize) {
      this.queryInfo.pagesize = newsize
      this.getOrderList()
    },
    handleCurrentChange(newpage) {
      this.queryInfo.pagenum = newpage
      this.getOrderList()
    },
    // 点击按钮打开修改地址的对话框
    showAddrDialog() {
      this.addrDialogVisible = true
    },
    // 监听级联选择器选中变化事件函数
    handleChange() {
      console.log(this.addrForm)
    },
    // 监听修改地址表单的关闭事件函数
    addrDialogClosed() {
      // 重置表单数据
      this.$refs.addrFormRef.resetFields()
    },
    // 点击按钮打开物流信息对话框
    async showProgressDialog(orderInfo) {
      // 获取订单的物流信息
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单物流信息数据失败！')
      }
      this.progressinfo = res.data
      this.progressDialogVisible = true
    }
  }
}
</script>
<style lang="less">
.el-cascader {
  width: 100%;
}
</style>
